// 渐变圆
/**
 * 已修改从传统js转ts
 * @create zgf
 */
let Cesium:any=undefined
/**
 * 渐变圆材质
 * @global
 * @param {object} options
 * @param {number} options.duration - 持续时长
 * @param {Color} options.color - 颜色
 * @see {@link module:Graphics#createFadeCylinderGraphics|createFadeCylinderGraphics}
 * @example
 const  circleFadeMaterial = new Cesium.Scene.CircleFadeMaterialProperty({
        color:   Cesium.Color.fromCssColorString("#02ff00"),
        duration:  2000,
      });
 */
export class CircleFadeMaterialProperty extends Cesium.Property{
   private _definitionChanged: any;
    private   _color: any;
    private  _colorSubscription: any;
    private _duration: any;
    private _time: any;

    constructor(options: any) {
        super()
        options = Cesium.defaultValue(options, Cesium.defaultValue.EMPTY_OBJECT);
        this._definitionChanged = new Cesium.Event();
        this._colorSubscription = void 0;
        this._color = Cesium.defaultValue(options.color, this._color) ||  new Cesium.Color(0, 0, 0, 0);
        this._duration = options.duration || 1e3;
        this._time = void 0;
    }
    // @ts-ignore
     get  isConstant(){
        return false;
     }
    // @ts-ignore
    get definitionChanged(): any {
        return this._definitionChanged;
    }

    get color() {return this._color;}
    set color(value: any) {this._color = value;}

    getType() { //  CircleFadeMaterial
        // @ts-ignore
        return Cesium.Material.CircleFadeMaterialType;
    }

    getValue(time: any, result: any) {
        if (!result) {
            result = {};
        }
        (result.color = Cesium.Property.getValueOrClonedDefault(this._color, time, this._color, result.color)),
        void 0 === this._time && (this._time = new Date().getTime()),
            (result.time = (new Date().getTime() - this._time) / this._duration);
        return result;
    }

    equals(other: any) {
        return (
            this === other || (other instanceof CircleFadeMaterialProperty && Cesium.Property.equals(this._color, other._color))
        );
    }
}
function  _getCircleFadeShader(options:any) {
    if (options && options.get) {
        return `czm_material czm_getMaterial(czm_materialInput materialInput)\n                
                  {\n                    
                      czm_material material = czm_getDefaultMaterial(materialInput);\n                    
                      material.diffuse = 1.5 * color.rgb;\n                    
                      vec2 st = materialInput.st;\n                    
                      float dis = distance(st, vec2(0.5, 0.5));\n                    
                      float per = fract(time);\n                    
                      if(dis > per * 0.5){\n                        
                          //material.alpha = 0.0;\n                        
                          discard;\n                    
                      }else {\n                            
                          material.alpha = color.a  * dis / per / 2.0;\n                    
                      }\n                    
                      return material;\n                
                  }`;
    }
}
const  _installCircleFadeMaterial=(CesiumSources:any)=> {
    Cesium=CesiumSources;
    // @ts-ignore
    Cesium.Scene.CircleFadeMaterialProperty = CircleFadeMaterialProperty;
    // @ts-ignore
    Cesium.Material.CircleFadeMaterialType = "CircleFadeMaterial";
    // @ts-ignore
    Cesium.Material._materialCache.addMaterial(Cesium.Material.CircleFadeMaterialType, {
        fabric: {
            // @ts-ignore
            type: Cesium.Material.CircleFadeMaterialType,
            uniforms: {
                color: new Cesium.Color(1, 0, 0, 1),
                time: 1,
            },
            source: _getCircleFadeShader({
                get: true,
            }),
        },
        translucent: function () {
            return !0;
        },
    });
}

export {
    _installCircleFadeMaterial
}
